//
//  LaunchViewController.m
//  nonghubao
//
//  Created by 王 文彬 on 13-1-26.
//  Copyright (c) 2013年 mypocketbaby. All rights reserved.
//

/*
 SUMMARY
 
 本类是启动引导类，本类显示一个欢迎封面图片，并检查是否保存有登录信息
 
 》检测是否保存得有用户名和密码：
   如果有，并通过网络立即验证，验证通过，就发出通知，告诉AppDelegate，启动MainViewController了
   如果没有，或验证失败，就发出通知，告诉AppDelegate，启动DefaultViewController

*/


#import "LaunchViewController.h"
#import "DBKit.h"
#import "Option.h"
#import "JMFBase.h"
#import "Helper.h"
#import "SplashViewController.h"

@interface LaunchViewController ()
{
    SplashViewController*    _splashViewController;
}
@end

@implementation LaunchViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
//    UIImage* launchImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Default" ofType:@"png"]];
//    UIImageView* launchImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -20, 320, [UIScreen mainScreen].bounds.size.height)];
//    launchImageView.image = launchImage;
//    
//    [self.view addSubview:launchImageView];
    
    
    
    //检查是否有手机号码和密码被保存了
    DBKit* dbkit = [DBKit sharedDBKit];
    Option* idOption = [dbkit optionWithName:@"accountID"];
    Option* tokenOption = [dbkit optionWithName:@"accountToken"];
    //保存用户token为全局
    [[NSUserDefaults standardUserDefaults] setValue:tokenOption.value forKey:@"accountToken"];
    
    //如果有就与服务器进行登录验证。由异步请求的协议方法负责根据服务器返回结果，处理后续流程
    if (tokenOption && idOption && tokenOption.value.length > 0 && idOption.value.length > 0) {

        JSONRequest* request = [[JSONRequest alloc] initWithURLString:_JSON_URL_U_GET_BASICINFO];
        request.delegate = self;
        [request setValue:tokenOption.value forParameter:@"token"];
        [request setValue:idOption.value forParameter:@"userId"];
        [request asynchronous];
    }
    
    //否则就通知AppDelegate启动DefaultViewController
    else {
   
        [[NSNotificationCenter defaultCenter] postNotificationName:@"Launcher_Finished" object:@1];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - JSONRequestDelegate

// 请求成功
//
- (void)requestDidLoadJSON:(NSDictionary *)dict
{
    JMFBase* jmf = [[JMFBase alloc] initWithJSONDictionary:dict];
    
    //登录成功就启动Main
    if (jmf.status == JMStatusSucceed) {

        //保存号码
        [Helper saveMobile:jmf.data[@"mobile"]];
        
        [[NSUserDefaults standardUserDefaults] setValue:jmf.data[@"realName"] forKey:@"accountName"];
#warning 激光注册，应该和LoginFormVC中的，封装到一个方法里面
        //极光推送服务
        //注销以前的设置，再设置当前登录后的用户的推送服务
        [APService setTags:nil alias:@""];
        [APService setTags:nil alias:[NSString stringWithFormat:@"%i", [_ACCOUNT_ID intValue]]];
        
        NSLog(@"LaunchVC:106 设置APService的Alias为：%i", [_ACCOUNT_ID intValue]);
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"Launcher_Finished" object:@2];
    }
    //无论是登录失败，还是其它错误，一律引导到Default
    else {

        [[NSNotificationCenter defaultCenter] postNotificationName:@"Launcher_Finished" object:@1];
    }
}


// 请求失败
//
- (void)requestFailWithNetworkUnavailabe
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Launcher_Finished" object:@1];
}

@end
